﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.animations;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GameEngine.Scenes
{
    public class Text: Actor
    {
        public SpriteFont font;
        public string text;
        public Color color;
        public byte originalAlpha;
        public int freqFlash = 500;
        public int timeElapsed;
        private bool _isFlashing;
        public bool isFlashing 
        {
            get { return _isFlashing; }
            set 
            {
                _isFlashing = value;
                timeElapsed = 0;
            }
        }


        public Text(SpriteFont font, string text, Vector2 position, Color color, OriginType originType):
            base(position,0,1,originType)
        {
            this.font = font;
            this.text = text;
            this.color = color;
            this.originalAlpha = color.A;
        }
        public Text(SpriteFont font, string text, Color color, OriginType originType) :
            this(font, text, new Vector2(), color, originType)
        {
        }
        public override void draw(SpriteBatch spriteBatch)
        {
            if (font != null && color != null)
                spriteBatch.DrawString(font, text, position, color);
        }
        public override void update(int dTime)
        {
            if (isFlashing)
            {
                timeElapsed += dTime;
                timeElapsed = timeElapsed % freqFlash;
                int alpha = (int)(Math.Abs(((float)timeElapsed / freqFlash) - 0.5f) * 2 * 255); //at half timeElapsed alpha becomes 0, at 0% or 100% timeElapsed alpha becomes 255
                color.A = (byte)alpha;
            }
            else
                color.A = originalAlpha;
            base.update(dTime);
        }
    }
}
